﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Veteran_cms.Models;
using Veteran_cms.HtmlHelpers;
using System.Text;
namespace Veteran_cms.Controllers
{
    [HandleError]
    public class ManageUserController : BaseController
    {
        
        //
        // GET: /userManagerment/
        Veterandb ctx = new Veterandb();
        public int PageSize = 6;
        #region--Admin--
        public ActionResult IndexUser(int page = 1)
        {
            //if (!IsAuthenticateAdmin())
            //    return RedirectToAction("LogOn", "DashBoard");
            var model = new HomeAdminListViewModel();
            IEnumerable<core_user> users = ctx.core_user.ToList<core_user>();
            model.UserList = users.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.PageInfo = new THS.HtmlHelpers.PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = users.Count() };
            return View(model);

        }
        //pagign
        public ActionResult Paging(int page = 1)
        {
            //if (!IsAuthenticateAdmin())
            //    return RedirectToAction("LogOn", "DashBoard");
            var model = new HomeAdminListViewModel();
            IEnumerable<core_user> users = ctx.core_user.ToList<core_user>();
            model.UserList = users.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.PageInfo = new THS.HtmlHelpers.PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = users.Count() };
            return PartialView("UCUserList",model);

        }
        //
        // GET: /userManagerment/Details/5

        public ActionResult Details(int id)
        {
            try
            {
                 //if (!IsAuthenticateAdmin())
                 // return RedirectToAction("LogOn", "DashBoard");
                 core_user adm = ctx.core_user.Single(p => p.user_id == id);
                if (adm != null)
                    return View(adm);
                return View();
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /userManagerment/Create

        public ActionResult Create()
        {
        //    if (!IsAuthenticateAdmin())
        //        return RedirectToAction("LogOn", "DashBoard");
            //User user = new User();
            return View();
        }

        //
        // POST: /userManagerment/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create([Bind(Exclude = "user_id")] core_user us)
        {
            // Attempt to register the user


            //if (!IsAuthenticateAdmin())
            //    return RedirectToAction("LogOn", "DashBoard");
            try
            {

                IEnumerable<core_user> existuser = ctx.core_user.Where(p => p.user_name == us.user_name).ToList();
                    if (existuser.Count() == 0)
                    {
                        EnCrypto enc = new EnCrypto();
                        us.password = enc.Encrypt(us.password);
                        us.created_date = DateTime.Now;
                         us.is_active = false;
                        ctx.AddTocore_user(us);
                        ctx.SaveChanges();
                        return RedirectToAction("IndexUser");
                    }
                    else
                    {
                        ViewData["Msg"] = "Đã tồn tại tên đăng nhập";
                        return View();

                    }
            }
            catch
            {
                return View("Create");
            }

        }

        //check esxist user name
        public bool CheckName(string userName)
        {
            try
            {
                core_user user = ctx.core_user.Where(p => p.user_name.Trim() == userName.Trim()).SingleOrDefault() as core_user;
                if (user != null)
                    return false;
                else
                    return true;
            }
            catch
            {
                return false;
            }
        }
        public bool CheckEmail(String email)
        {
            try
            {
                core_user user = ctx.core_user.Where(p => p.email.Trim() == email.Trim()).SingleOrDefault() as core_user;
                if (user!=null)
                    return false;
                else
                    return true;
            }
            catch
            {
                return false;
            }
        }

        //
        // POST: /userManagerment/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Details(int id, FormCollection collection)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOn", "DashBoard");
                core_user adm = ctx.core_user.Where(ad => ad.user_id == id).SingleOrDefault();
                if (adm != null)
                {
                    
                    EnCrypto crt = new EnCrypto();
                    adm.password = crt.Encrypt(collection["Password"]);
                    ctx.SaveChanges();
                    String passDecript = crt.Decrypt(adm.password);
                    if(MailHelper.SendByTemplate("RePassword","Mật khẩu mới của bạn như sau:"+passDecript, adm.email))

                    ViewData["Msg"] = "Cập nhật và gởi mail thành công!";
                    else
                        ViewData["Msg"] = "Đã Cập nhật nhưng gởi mail không thành công!";
                    return View("Details", adm);
                }
                return RedirectToAction("Details");
            }
            catch
            {
                ViewData["Msg"] = "Cập nhật không thành công";
                return View("Details");
            }
        }
        //allow Log On
        public void ActiveUser(int id)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //  return;
                core_user adm = ctx.core_user.Where(p => p.user_id == id).SingleOrDefault() as core_user;
               if (adm.is_active)
                   adm.is_active = false;
                else
                   adm.is_active = true;
                ctx.SaveChanges();


            }
            catch
            {
                return;
            }
        }
        //active,deactivate logon
        //deactive
        public ActionResult DeactivateUsers(int id)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOn", "DashBoard");
                core_user us = ctx.core_user.Where(p => p.user_id != id) as core_user;
                if (us != null)
                {
                    us.is_active = false;
                    ctx.SaveChanges();
                }
                return RedirectToAction("Index");

            }
            catch
            {
                return RedirectToAction("Index");
            }
        }
     
        //
        // GET: /userManagerment/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOn", "DashBoard");
                core_user adm = ctx.core_user.Where(p => p.user_id == id).SingleOrDefault();
                ctx.core_user.DeleteObject(adm);
                ctx.SaveChanges();
                return RedirectToAction("IndexUser");
            }
            catch
            {
                return View();
            }
        }


        #endregion


    }
}
